<?php
  class View{
    
    static $langs = array('EN', 'DE');
    static $lang;
    static $html;
    static $site;
    
    static function prepare($site){
      View::$html = file_get_contents('./view/template.html');
      View::$site = $site;
      View::$lang = View::$langs[0];
    }
    
    static function change_language($newL){
      View::$lang = $newL;
    }
    
    static function insert_languages(){
      $pos = false;
      for($i = 0; $i < count(View::$langs) && $pos === false; $i ++)
        if(View::$langs[$i] == View::$lang)
          $pos = $i;
      if($pos === false)
        $next = View::$langs[0];
      else
        if($pos >= count(View::$langs)-1)
          $next = View::$langs[0];
        else
          $next = View::$langs[$pos+1];
      View::$html = str_replace('[BODY]', '<a id="lang_change" href="?lang='.$next.'&site='.View::$site.'">'.$next.'</a>' . "\r\n[BODY]", View::$html);
    }
    
    static function insert_header(){
      View::$html = str_replace('[HEADER]', file_get_contents('./view/template_header.html'), View::$html);
      View::$html = str_replace('[LOGO]', '<a href="?lang='.View::$lang.'&site=home"><logo></logo></a>', View::$html);
      $titles =  array(
                        'LAN',
                        'DEV',
                        'WEB'
                      );
      $links = array(
                      array('Vote', 'Queue', 'Chat'),
                      array('Work'),
                      array('Guestbook', 'Layouts', 'Timer', 'Board', 'Blog')
                    );
      $tit = '';
      for($i = 0; $i < count($titles); $i ++)
        $tit .= '<li><a href="?lang='.View::$lang.'&site='.strtolower($titles[$i]).'" onmouseout="window.clearTimeout(navTimeoutStartOpen)" onmouseover="navTimeoutStartOpen = window.setTimeout(\'openNavigation('.$i.')\', 500)">'.$titles[$i]."</a></li>\r\n";
      $lin = '';
      for($i = 0; $i < count($links); $i ++){
        $lin .= "<ul id=\"navigationList_$i\">\r\n";
        foreach($links[$i] as $l)
          $lin .= '  <li><a href="?lang='.View::$lang.'&site='.strtolower($l).'">'.$l."</a></li>\r\n";
        $lin .= "</ul>";
      }
      View::$html = str_replace('[NAV_TITLES]', $tit, View::$html);
      View::$html = str_replace('[NAV_LINKS]', $lin, View::$html);
    }
    
    static function insert_footer(){
      View::$html = str_replace('[FOOTER]', file_get_contents('./view/template_footer.html'), View::$html);
      if(View::$lang == 'DE')
        $foots = array('&Uuml;ber uns', 'Kontakt', 'Impressum');
      else
        $foots = array('Ourselves', 'Contact', 'Imprint');
      $foot_sites = array('ourselves', 'contact', 'imprint');
      for($i = 0; $i < count($foots) && $i < count($foot_sites); $i ++)
        View::$html = str_replace('[FOOTS]', '<a href="?lang='.View::$lang.'&site='.$foot_sites[$i].'">'.$foots[$i].'</a>' . "\r\n[FOOTS]", View::$html);
    }
    
    static function add_head($h){
      View::$html = str_replace('[HEAD]', $h . "\r\n[HEAD]", View::$html);
    }
    
    static function insert_content(){
      $site = preg_replace('/[\/-\s]/', '_', strtolower(View::$site));
      if(!preg_match('/^\w+$/', $site))
        $site = 'home';
      if(is_file('./Sites/'.$site.'_'.View::$lang.'.html'))
        $site .= '_'.View::$lang;
      else if(is_file('./Sites/'.$site.'_EN.html'))
        $site .= '_EN';
      else if(is_file('./Sites/'.$site.'_DE.html'))
        $site .= '_DE';
      else{
        $site = 'home_'.View::$lang;
        if(!is_file('./Sites/'.$site.'.html'))
          $site = 'home_EN';
      }
      View::$html = str_replace('[CONTENT]', file_get_contents('./Sites/'.$site.'.html') . "\r\n[CONTENT]", View::$html);
    }
    
    static function print_site(){
      echo preg_replace('/\[.+\]/', '', View::$html);
    }
    
  }
?>